package br.com.amadeus.android.dao;

import br.com.amadeus.android.entities.Config;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class ConfigDAO {
	
	public static String[] columns = new String[] {ConfigDAO.ID, ConfigDAO.AMADEUS_URL, ConfigDAO.USER_LOGIN,
		ConfigDAO.USER_PASSWORD, ConfigDAO.USER_NAME, ConfigDAO.VERSION_SYNC};
	
	public static final String ID = "_id";
	public static final String AMADEUS_URL = "amadeus_url";
	public static final String USER_LOGIN = "user_login";
	public static final String USER_PASSWORD = "user_password";
	public static final String USER_NAME = "user_name";
	public static final String VERSION_SYNC = "version_sync";
	
	private SQLiteDatabase db;
	
	private static ConfigDAO instance = new ConfigDAO();
	
	public static ConfigDAO getInstance(Context ctx){
		
		if (instance.db == null || !instance.db.isOpen()) {
			instance.db = new DBHelper(ctx).getWritableDatabase();
		}
		return instance;
	}
	
	private ConfigDAO(){
		
	}
	
	public long save(Config config){
		long id = config.getId();
		if (id == 0) {
			return insertConfig(config);
		}else{
			return update(config);
		}
	}
	
	private long insertConfig(Config config){
		long id_config = -1;

		db.beginTransaction();
		try{
			ContentValues values = new ContentValues();
			values.put(ConfigDAO.AMADEUS_URL, config.getAmadeus_url());
			values.put(ConfigDAO.USER_LOGIN, config.getUser_login());
			values.put(ConfigDAO.USER_PASSWORD, config.getUser_password());
			values.put(ConfigDAO.USER_NAME, config.getUser_name());
			values.put(ConfigDAO.VERSION_SYNC, config.getVersionSync());
			
			id_config = db.insert(DBHelper.TABLE_CONFIG, null, values);
			
			if (id_config != -1) {
				db.setTransactionSuccessful();
			}
		}finally {
			db.endTransaction();
		}
		
		return id_config;
	}
	
	private long update(Config config){
		boolean deuErro = false;
		db.beginTransaction();
		try{
			ContentValues values = new ContentValues();
			values.put(ConfigDAO.AMADEUS_URL, config.getAmadeus_url());
			values.put(ConfigDAO.USER_LOGIN, config.getUser_login());
			values.put(ConfigDAO.USER_PASSWORD, config.getUser_password());
			values.put(ConfigDAO.USER_NAME, config.getUser_name());
			values.put(ConfigDAO.VERSION_SYNC, config.getVersionSync());
			
			deuErro = db.update(DBHelper.TABLE_CONFIG, values, ConfigDAO.ID + " = ?",
					new String[]{String.valueOf(config.getId())}) == 0;
			
			if (!deuErro)
				db.setTransactionSuccessful();
		}finally {
			db.endTransaction();
		}
		
		return config.getId();
	}
	
	public Config getConfig(){
		Config config = null;
//		Cursor c = db.query(DBHelper.TABLE_CONFIG, columns, null, null, null, null, null);
		Cursor c = db.query(DBHelper.TABLE_CONFIG, columns, "_id = 2", null, null, null, null);
		if (c.moveToFirst()) {
			int id_id = c.getColumnIndex(ConfigDAO.ID);
			int id_amadeus_url = c.getColumnIndex(ConfigDAO.AMADEUS_URL);
			int id_user_login = c.getColumnIndex(ConfigDAO.USER_LOGIN);
			int id_user_password = c.getColumnIndex(ConfigDAO.USER_PASSWORD);
			int id_user_name = c.getColumnIndex(ConfigDAO.USER_NAME);
			int id_version_sync = c.getColumnIndex(ConfigDAO.VERSION_SYNC);
			
			do {
				
				int id = c.getInt(id_id);
				String amadeus_url = c.getString(id_amadeus_url);
				String user_login = c.getString(id_user_login);
				String user_password = c.getString(id_user_password);
				String user_name = c.getString(id_user_name);
				int versionSync = c.getInt(id_version_sync);
				
				config = new Config(id, amadeus_url, user_login, user_password, user_name, versionSync);
				
			} while (c.moveToNext());
		}
		c.close();
		return config;
	}

}
